﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

/// <summary>
/// BaseInfoDB 的摘要说明
/// </summary>
public sealed class BaseInfoDB
{
    static readonly string CONNECTION_STRING = ConfigurationManager.ConnectionStrings["SMS_Online"].ConnectionString;
    public static List<Store> GetStoresByRegionID(string RegionID)
    {
        List<Store> list = new List<Store>();
        list.Clear();
        using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
        {
            using (SqlCommand com=new SqlCommand())
            {
                com.CommandText = string.Format("exec usp_GetStoresByRegionID '{0}'",RegionID);
                com.Connection= con;
                con.Open();
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    list.Add(new Store(reader["StoreID"].ToString(), reader["CName"].ToString()));
                }

                reader.Close();
                if (list.Count>0)
                {
                    list.Insert(0, new Store("All", "全部"));
                }
            }
        }
        return list;
    }

    public static List<Region> GetRegions()
    {
        List<Region> list = new List<Region>();
        using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
        {
            using (SqlCommand com = new SqlCommand())
            {
                com.CommandText = "exec usp_GetRegions";
                com.Connection = con;
                con.Open();
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    list.Add(new Region(reader["RegionID"].ToString(), reader["CRegionName"].ToString()));
                }
                reader.Close();
            }
        }
        return list; 
    }
    public static List<Region> GetRegionsExceptHQ()
    {
        List<Region> list = new List<Region>();
        using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
        {
            using (SqlCommand com = new SqlCommand())
            {
                com.CommandText = "exec usp_GetRegionsExceptHQ";
                com.Connection = con;
                con.Open();
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    list.Add(new Region(reader["RegionID"].ToString(), reader["CRegionName"].ToString()));
                }
                reader.Close();
            }
        }
        return list; 
    }

    public static List<Brand> GetBrands()
    {
        List<Brand> list = new List<Brand>();
        using (SqlConnection con = new SqlConnection(CONNECTION_STRING))
        {
            using (SqlCommand com = new SqlCommand())
            {
                com.CommandText = "exec usp_GetBrands";
                com.Connection = con;
                con.Open();
                SqlDataReader reader = com.ExecuteReader();
                while (reader.Read())
                {
                    list.Add(new Brand(reader["BrandID"].ToString(), reader["CBrandName"].ToString()));
                }
                reader.Close();
            }
        }
        return list;
    }
}
